package com.example.obs.controller;


import com.alibaba.excel.EasyExcel;
import com.example.obs.config.UserDataListener;
import com.example.obs.model.domin.CurrentPage;
import com.example.obs.model.domin.User;
import com.example.obs.model.utils.ObsPage;
import com.example.obs.service.UserService.UserService;
import com.example.obs.utils.JsonUtils;
import com.example.obs.utils.PageUtils;
import com.example.obs.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@Api(tags = "用户管理接口")
@RequestMapping("/users")
@RestController
//@PreAuthorize("hasAnyAuthority('SP_ADMIN', 'ADMIN')")
public class UsersController {

    private final UserService userService;

    private final UserDataListener userDataListener;

    public UsersController(UserService userService, UserDataListener userDataListener) {
        this.userService = userService;
        this.userDataListener = userDataListener;
    }

    /**
     * 查询用户列表
     * @param data
     * @return
     */
    @ApiOperation(value = "查询用户列表")
    @RequestMapping(value="/selectUserList",method= RequestMethod.POST)
    public ObsPage selectUserList(@RequestBody Map<String,Object> data) throws Exception {
        User user= JsonUtils.map2obj((Map<String,Object>) data.get("user") ,User.class);
        CurrentPage currentPage=JsonUtils.map2obj((Map<String,Object>)data.get("page"),CurrentPage.class);
        List<User> users= userService.selectUsers(user);
        int total=users.size();
        return new ObsPage(total, PageUtils.pagehelper(users,currentPage));
    }
    /**
     * 添加用户
     * @param user
     * @return
     */
    @ApiOperation(value = "添加用户")
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public int addUser(@RequestBody User user) {
        return userService.addUser(user);
    }
    /**
     * 修改用户状态
     * @param user
     * @return
     */
    @ApiOperation(value = "修改用户状态")
    @RequestMapping(value="/changeState",method= RequestMethod.POST)
    public int changeState(@RequestBody User user) {
        userService.changeState(user);
        return 1;
    }
    /**
     * 删除用户
     * @param id
     * @return
     */
    @ApiOperation(value = "删除用户",notes ="不删除用户文件" )
    @RequestMapping(value="/deleteUser",method= RequestMethod.POST)
    public int deleteUser(@RequestBody Integer id) {
        userService.deleteUser(id);
        return 1;
    }

    @RequestMapping(value = "/resetPasswordUser")
    public int resetPassword(@RequestBody Integer id){
        User user = new User();
        user.setId(id);
        String password= DigestUtils.md5DigestAsHex("123456".getBytes());
        user.setPassword(password);
        userService.updateUser(user);
        return 1;
    }
    @ApiOperation(value = "修改用户角色")
    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    public int updateRole(@RequestBody Integer id){
        return 1;
    }

    @ApiOperation(value = "批量导入用户")
    @PostMapping("/upload")
    public ResultVO upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), User.class, userDataListener).sheet().doRead();
        return ResultVO.success();
    }

    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public int updateUser(@RequestBody User user){
        userService.updateUser(user);
        return 1;
    }

}
